import { BASE_URL } from '../../common/config';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banners:[
    {path: "/images/banner/banner3.png"},
    {path: "/images/banner/banner2.png"},
    {path: "/images/banner/banner4.png"},
    {path: "/images/banner/1733800574424.png"}],
    current:0,
    page:1,
    nowLabel:'推荐',
    bookListFather:[],
    noMore:false
  },
  onSwiperChange: function (e) {
    // console.log(e)
    if (e.detail.source == "autoplay" || e.detail.source == "touch") {
      this.setData({
        current: e.detail.current
      });
    }
  },
  jumpSearch(){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  onChange(event) {
    const classify=this.selectComponent('#classify')
    this.setData({
      nowLabel:event.detail.title
    })
    if(event.detail.title=="分类"){
      classify.getCategroyBooks();
    }else if(event.detail.title=="排行"){
      this.getRankBooks(1)
    }
  },
  getRankBooks(page){
    wx.request({
      url: `${BASE_URL}/getRankBooks`,
      method:'POST',
      data:{
        page:page,
        pageSize:6
      },
      success:(res)=>{
        // console.log("请求成功：",res.data)
        if(res.data==""||res.data.length!=6){
          this.setData({
            noMore:true
          })
        }
        let getBookList=res.data
      let bookListFather=this.data.bookListFather
      bookListFather=(bookListFather || []).concat(getBookList);
        this.setData({
          bookListFather:bookListFather
        })
      },
      fail:(err)=>{}
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.getCategroyBooks()
    this.selectComponent('#recommend').getRecommendBooks(1);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    // console.log("index页触发触底")
    const nowLabel=this.data.nowLabel
    if(nowLabel=='推荐'){
    this.selectComponent('#recommend').onRecommendBottom();
    }else if(nowLabel=="分类"){
      this.selectComponent('#classify').onClassifyBottom();
    }else if(nowLabel=="排行"){
      let page=this.data.page+1
      if(!this.data.noMore){
        this.setData({
          page:page
        })
        // console.log(this.data.page)
        this.getRankBooks(page)
      }else{
        console.log("没有更多数据了")
      }
      
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})